const Mock = require('mockjs')
import qs from 'qs'

const Random = Mock.Random

const newsData = function() {
  const articles = []
  for (let i = 0; i < 10; i++) {
    const newsObj = {
      title: Random.csentence(5, 12),
      date: Random.date()
    }
    articles.push(newsObj)
  }

  return {
    articles: articles
  }
}

const loginData = function(option) {
  console.log('打印登录接口接收参数-----', option)
  const { name, password } = qs.parse(option.body)
  const response = {
    data: 'other',
    flag: true,
    msg: '操作成功！'
  }
  if (name === '1' && password === '1') {
    response.data = 'admin'
  }
  return response
}

const totalCountData = function() {
  console.log('请求接口')
  const response = {
    data: [
      {
        key: '总销售额',
        value: Random.integer(60, 10000),
        detail: '说明文字',
        today: '今日销售额：¥' + Random.integer(0, 1000),
        isMoney: true,
        isup: Random.integer(1, 2)
      },
      {
        key: '访问数',
        value: Random.integer(60, 10000),
        detail: '说明文字',
        today: '今日访问量：' + Random.integer(0, 1000),
        isup: Random.integer(1, 2)
      },
      {
        key: '下单数',
        value: Random.integer(60, 10000),
        detail: '说明文字',
        today: '今日下单数：' + Random.integer(0, 100),
        isup: 0
      },
      {
        key: '退款数',
        value: Random.integer(60, 10000),
        detail: '说明文字',
        today: '今日退款数：' + Random.integer(0, 100),
        isup: 0
      }
    ],
    flag: true,
    msg: '操作成功！'
  }
  return response
}

const trendData = function(option) {
  console.log('打印趋势接口接收参数-----', option)
  const { type } = qs.parse(option.body)
  const list = []
  for (let i = 0; i < 12; i++) {
    const newsObj = {
      dateTime: type === 'third' ? ((i + 1) + '月') : Random.date(),
      totalPrice: Random.integer(60, 10000),
      clickNum: Random.integer(60, 10000),
      peopleNum: Random.integer(60, 10000),
      returnNum: Random.integer(60, 10000),
      returnPrice: Random.integer(60, 10000)
    }
    list.push(newsObj)
  }

  const response = {
    data: list,
    flag: true,
    msg: '操作成功！'
  }
  return response
}

const rankData = function() {
  const list = []
  for (let i = 0; i < 5; i++) {
    const newsObj = {
      talkDuration: Random.cword(5, 10),
      percentage: Random.integer(60, 10000),
      order: Random.integer(60, 100)
    }
    list.push(newsObj)
  }
  list.sort(function(a, b) {
    return a.percentage - b.percentage
  })

  const response = {
    data: list,
    flag: true,
    msg: '操作成功！'
  }
  return response
}

const pieData = function() {
  const response = {
    data: [
      { key: '生活用品', value: 40 },
      { key: '电器类', value: 5 },
      { key: '美妆用品', value: 30 },
      { key: '服饰箱包', value: 10 },
      { key: '票务', value: 12 },
      { key: '其他', value: 3 }
    ],
    flag: true,
    msg: '操作成功！'
  }
  return response
}

const authData = function(option) {
  console.log('打印权限路由接收参数-----', option)
  const { role } = qs.parse(option.body)
  let data = {}
  if (role === 'admin') {
    data = {
      lv1: ['001', '002', '003'],
      lv2: ['001a', '002a', '002b', '002c', '003a'],
      lv3: ['001aa']
    }
  } else {
    data = {
      lv1: ['001', '003'],
      lv2: ['001a', '003a'],
      lv3: ['001aa']
    }
  }
  const response = {
    data: data,
    flag: true,
    msg: '操作成功！'
  }
  return response
}

Mock.mock('/news/api', 'post', newsData)
Mock.mock('/user/Login', 'post', loginData)
Mock.mock('/data/countData', 'get', totalCountData)
Mock.mock('/data/trendData', 'post', trendData)
Mock.mock('/data/rankData', 'get', rankData)
Mock.mock('/data/pieData', 'get', pieData)
Mock.mock('/data/authData', 'post', authData)
